#include <iostream>
#include <pthread.h>
#include "Mutex.hpp"
#include "Cond.hpp"

using namespace MutexModule;
using namespace CondModule;

char arr1[5] = "1234"; // 包含'/0'
char arr2[5] = "abcd";

Mutex mutex;
Cond cond;
bool flag = true; 

void *Func1(void *arg)
{
    for(const auto& e : arr1)
    {
        LockGuard lockguard(mutex);
        while(!flag)
            cond.Wait(mutex);
        std::cout << e;
        flag = !flag;
        cond.Signal();
    }
    return nullptr;
}

void *Func2(void *arg)
{
    for(const auto& e : arr2)
    {
        LockGuard lockguard(mutex);
        while(flag)
            cond.Wait(mutex);
        std::cout << e;
        flag = !flag;
        cond.Signal();
    }
    return nullptr;
}

int main()
{
    pthread_t tid1, tid2;

    pthread_create(&tid1, nullptr, Func1, nullptr);
    pthread_create(&tid2, nullptr, Func2, nullptr);

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);

    std::cout << std::endl;
    
    return 0;
}